package com.aaa.qy127.controller;

import com.aaa.qy127.entity.Dl;
import com.aaa.qy127.entity.Role;
import com.aaa.qy127.service.DlService;
import com.aaa.qy127.service.StaffService;
import com.aaa.qy127.util.CodeAndMsg;
import com.aaa.qy127.util.DuanXin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RequestMapping("/user")
@RestController
public class DlController {
    @Resource(name = "DlServiceImpl")
    private DlService dlService;

    /**
     * 前端登录页面
     * @param phone
     * @param password
     * @param request
     * @return
     */
    @RequestMapping("/login")
    public CodeAndMsg login(String phone, String password, HttpServletRequest request){
        CodeAndMsg codeAndMsg = dlService.getCodeAndMsg(phone, password,request);
        return codeAndMsg;
    }

    /**
     * 添加用户信息
     * @param dl
     * @return
     */
    @RequestMapping("/addUser")
    public CodeAndMsg addUser(Dl dl){
        CodeAndMsg codeAndMsg = dlService.InsDl(dl);
        return codeAndMsg;
    }

    /**
     * 短信验证码
     * @param phone
     * @return
     */
    @RequestMapping("/yanzhengma")
    public int yanzhengma(String phone,HttpServletRequest request){
        int i = DuanXin.dx(phone);
        request.getSession().setAttribute("yanzhengma",i);
        return i;
    }

    /**
     * 向 用户表中插入一条数据
     * 先判断 用户表中 是否 已经存在
     * 存在 就不执行插入 操作
     * 不存在 就执行 插入 操作
     * @param dl
     * @return
     */
    @RequestMapping("/zhuce")
    public CodeAndMsg zhuce(Dl dl,HttpServletRequest request){
        CodeAndMsg zhuce = dlService.zhuce(dl,request);
        return zhuce;
    }

    /**
     * 通过 userid  修改 该用户 信息
     * 然后重新 根据 该用户的 userid 查询 一遍
     * @param dl
     * @return
     */
    @RequestMapping("/updateOneUserByUserid")
    public CodeAndMsg updateOneUserByUserid(Dl dl,HttpServletRequest request){
        CodeAndMsg codeAndMsg = dlService.updateOneUserByUserid(dl, request);
        return  codeAndMsg;
    }

    /**
     * 通过 该用户的 userid 修改 该用户的 密码
     * @param password
     * @param userid
     * @return
     */
    @RequestMapping("/updatePasswordByUserid")
    public CodeAndMsg updatePasswordByUserid(String password,String userid){
        CodeAndMsg codeAndMsg = dlService.updatePasswordByUserid(password, userid);
        return codeAndMsg;
    }

}
